package com.voute.Presenter.Impl;

import android.text.TextUtils;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.CreateFeedResposneBean;
import com.voute.Model.Bean.RecommendCirclesBean;
import com.voute.Model.ICreateFeedModel;
import com.voute.Model.Impl.CreateFeedModelImpl;
import com.voute.Presenter.IPublishFeedPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.PreferenceUtils;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.ICreateFeedView;

import java.util.HashMap;

import rx.functions.Action0;

/**
 * Created by liucxu on 2017/4/27.
 */

public class PublishFeedPresenterImpl implements IPublishFeedPresenter {

    private ICreateFeedView circleView;
    private ICreateFeedModel model;

    public PublishFeedPresenterImpl(ICreateFeedView view) {
        circleView = view;
        model = new CreateFeedModelImpl();
    }

    @Override
    public void queryRecommandCircles(String q) {
        model.queryRecommandCircles(q).compose(TransformUtils.<RecommendCirclesBean>defaultSchedulers()).subscribe(new CommonSubscriber<RecommendCirclesBean>() {
            @Override
            public void onSuccess(RecommendCirclesBean response) {
                circleView.setRecommandCircles(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                circleView.showError(response.getMsg());
            }
        });
    }

    @Override
    public void CreateFeed(String title, String desc, String pic, String left, String right, String circle_name, String circle_hash, String last_hour) {
        HashMap<String, String> params = new HashMap<>();
        params.put("title", title);
        if (!TextUtils.isEmpty(desc)) {
            params.put("description", desc);
        }
        if (!TextUtils.isEmpty(pic)) {
            params.put("pic", pic);
        }
        params.put("left", left);
        params.put("right", right);
        if(!TextUtils.isEmpty(circle_name)){
            params.put("circle_name", circle_name);
        }
        if(!TextUtils.isEmpty(circle_hash)){
            params.put("circle_hash", circle_hash);
        }
        if(!TextUtils.isEmpty(last_hour)){
            params.put("last_hour", last_hour);
        }
        params.put("lat", PreferenceUtils.GetLatitude());
        params.put("lng", PreferenceUtils.GetLongtitude());
        model.CreateFeed(params).compose(TransformUtils.<CreateFeedResposneBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                circleView.showLoading();
            }
        }).subscribe(new CommonSubscriber<CreateFeedResposneBean>() {
            @Override
            public void onSuccess(CreateFeedResposneBean response) {
                circleView.hideLoading();
                circleView.createFeedSuccess(response.getData().getHash());
            }

            @Override
            public void onFailure(BaseBean response) {
                circleView.hideLoading();
                circleView.showError(response.getMsg());
            }
        });
    }
}
